package com.gree.flowable.app.controller;

import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;

@Controller
public class FlowableBaseApi {
    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @GetMapping("/index")
    public ModelAndView index(@RequestParam(name = "auth") String auth, HttpServletResponse response){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("auth",auth);
        return modelAndView;
    }

    /**
     * @Title: 判断是否挂起状态 true 已挂起 false未挂起激活
     * @param: processInstanceId 流程实例id
     * @return
     */
    protected boolean isSuspended(String businessKey) {
        boolean flag = true;
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey(businessKey).singleResult();
        if (processInstance != null){
            flag = processInstance.isSuspended();
        }
        return flag;
    }

    protected String rufindPorcInsByKey(String businessKey){
        ProcessInstance instance = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey(businessKey).singleResult();
        return instance.getId();
    }

    protected String findActTaskIdByKey(String businessKey){
        Task task = taskService.createTaskQuery().processInstanceBusinessKey(businessKey).singleResult();
        return task.getId();
    }

}
